<?php

namespace App\Services;

use App\Models\Crm_Member_Allot;
use App\Models\Crm_Member_Delete;
use App\Models\Crm_Member_Follow;
use App\Models\Crm_Member_Log;
use App\Models\Crm_Member_Rotate;
use App\Models\Crm_Report;
use App\Traits\External;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class LogService
{
    public function followsLog($params)
    {
        $result = Crm_Member_Follow::getFilter($params)
            ->select(['id','mid','mode','intention','content','nexttime','imgs','optionid','optionname','created_at'])
            ->Nodel()
            ->paginate($params['pageSize']);

        foreach ($result as $key => $value) {
            $result[$key]['imgs'] = json_decode($value->imgs,true);
            $result[$key]['annex'] = $value->hasOneAnnex()->select(['id','content','optionid','optionname','created_at'])->Nodel()->first();
        }
        $list['total'] = $result->total();
        $result = External::optionResult($result,1);
        $list['list'] = $result;

        return $list;
    }

    public function rotatesLog($params)
    {
        $result = Crm_Member_Rotate::getFilter($params)
            ->select(['id','mid','explain','remark','optionid','optionname','created_at'])
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $result = External::optionResult($result,1);
        $list['list'] = $result;

        return $list;
    }

    public function allotLog($params)
    {
        $result = Crm_Member_Allot::getFilter($params)
            ->select(['id','mid','olduserid','oldusername','optionid','optionname','remark','created_at'])
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $result = External::optionResult($result,1);
        $list['list'] = $result;

        return $list;
    }

    public function deleteLog($params)
    {
        $result = Crm_Member_Delete::getFilter($params)->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $result = External::optionResult($result,1);
        $list['list'] = $result;

        return $list;
    }

    public function memberLog($params)
    {
        $result = Crm_Member_Log::getFilter($params)->Nodel()->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function memberLogDel($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();

            foreach ($ids as $id) {
                $res = Crm_Member_Log::find($id);
                $res->is_del = 'Y';
                if(!$res->save()){
                    throw new \Exception("删除Crm_Member_Log客户日志失败".$id);
                }
            }

            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
